package com.css.fxfzypg.modules.model.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.constants.FxfzConstants;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.similitudeAnalyse.utils.BeanUtil;
import com.css.fxfzypg.modules.model.entity.RescueForceEntity;
import com.css.fxfzypg.modules.baseData.repository.RescueForceRepository;
import com.css.fxfzypg.modules.model.service.RescueForceService;
import com.css.fxfzypg.modules.model.vo.RescueForceVO;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.util.UUIDGenerator;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import com.css.fxfzypg.zcpt.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @date 2022/9/28 11:23
 * @description
 **/
@Service
public class RescueForceServiceImpl implements RescueForceService {

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private RescueForceRepository rescueForceRepository;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private SysAreaService areaService;

    @Autowired
    SUserService sUserService;

    @Override
    public Map<String, Object> getPage(RescueForceVO rescueForceVO, int curPage, int pageSize) {
        Map<String,String> areaMap = (Map<String,String>)JSONObject.parse(redisTemplate.opsForValue().get(FxfzConstants.CACHE_SYS_KEY  +"areaMap"));
        Map<String, Object> page=rescueForceRepository.getPage(rescueForceVO,curPage,pageSize);
        List<RescueForceEntity> rescueForceEntityList = (List<RescueForceEntity>) page.get("rows");
        for(RescueForceEntity item:rescueForceEntityList){
            item.setProvince(areaMap.get(item.getProvince()));
            String city = item.getCity();
            if(!"全部".equals(city)){
                StringBuffer buffer = new StringBuffer();
                String[] split = city.split(",");
                for(String str:split){
                    buffer.append(areaMap.get(str)+",");
                }
                String cityStr=buffer.toString();
                item.setCity(cityStr.substring(0,cityStr.lastIndexOf(",")));
            }
        }
        return page;
    }

    @Override
    public void save(RescueForceEntity rescueForceEntity) {
        Map<String,String> orgMap = (Map<String,String>) JSONObject.parse(redisTemplate.opsForValue().get(FxfzConstants.CACHE_SYS_KEY  +"orgMap"));
        String userId = PlatformSessionContext.getUserID();
        rescueForceEntity.setId(UUIDGenerator.getUUID());
        rescueForceEntity.setCreateUser(userId);
        String user = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        rescueForceEntity.setCreateUserName(sUser.getUserName());
        rescueForceEntity.setCreateTime(new Date());
        rescueForceEntity.setOrgId(sUser.getOrgId());
        String orgName = orgMap.get(sUser.getOrgId());
        rescueForceEntity.setOrgName(orgName);
        rescueForceEntity.setDelFlag(YNEnum.N.toString());
        String areaCode = areaService.getDivisionCodeById(rescueForceEntity.getProvince());
        areaCode =(String) JSONObject.parse(areaCode);
        rescueForceEntity.setProvinceCode(areaCode);
        rescueForceRepository.save(rescueForceEntity);
    }

    @Override
    public RescueForceEntity getModelById(String id) {
        Map<String,String> areaMap = (Map<String,String>)JSONObject.parse(redisTemplate.opsForValue().get(FxfzConstants.CACHE_SYS_KEY  +"areaMap"));
        RescueForceEntity rescueForce=rescueForceRepository.getModelById(id);
        rescueForce.setProvince(areaMap.get(rescueForce.getProvince()));
        String city = rescueForce.getCity();
        if(!"全部".equals(city)){
            StringBuffer buffer = new StringBuffer();
            String[] split = city.split(",");
            for(String str:split){
                buffer.append(areaMap.get(str)+",");
            }
            String cityStr=buffer.toString();
            rescueForce.setCity(cityStr.substring(0,cityStr.lastIndexOf(",")));
        }
        return rescueForce;
    }

    @Override
    public RescueForceEntity getDefaultModel() {
        return rescueForceRepository.getDefaultModel();
    }

    @Override
    public List<RescueForceEntity> getByDivision(String province, String city, int isNa) {
        return rescueForceRepository.getByDivision(province,city,isNa);
    }

    @Override
    public void delete(String id) {
        RescueForceEntity rescueForceEntity = rescueForceRepository.getModelById(id);
        String userId = PlatformSessionContext.getUserID();
        if(userId.equals(rescueForceEntity.getCreateUser())){
            rescueForceRepository.delete(id);
        }else{
            throw new RuntimeException("删除失败，只能删除自己创建的模型");
        }

    }

    @Override
    public void update(RescueForceEntity rescueForce) {
        RescueForceEntity rescueForceEntity = rescueForceRepository.getModelById(rescueForce.getId());
        String userId = PlatformSessionContext.getUserID();
        if(userId.equals(rescueForceEntity.getCreateUser())){
            rescueForce.setUpdateUser(PlatformSessionContext.getUserID());
            rescueForce.setUpdateTime(new Date());
            rescueForceRepository.update(rescueForce);
        }else{
            throw new RuntimeException("更新失败，只能更新自己创建的模型");
        }
    }

    @Override
    public RescueForceVO getModelByIdForCopy(String id) {
        Map<String,String> orgMap = (Map<String,String>) JSONObject.parse(redisTemplate.opsForValue().get(FxfzConstants.CACHE_SYS_KEY  +"orgMap"));
        RescueForceEntity rescueForce = rescueForceRepository.getModelById(id);
        RescueForceVO vo = new RescueForceVO();
        BeanUtil.copy(rescueForce,vo);
        vo.setModelName("");
        vo.setIsDefault("0");
        vo.setCity("");
        String userId = PlatformSessionContext.getUserID();
        String user = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        vo.setCreateUser(userId);
        vo.setCreateUserName(sUser.getUserName());
        vo.setOrgId(sUser.getOrgId());
        vo.setOrgName(orgMap.get(sUser.getOrgId()));
        vo.setDelFlag(YNEnum.N.toString());
        return vo;
    }

    @Override
    public RescueForceEntity getDefaultModel(String taskId) {
        RescueForceEntity rescueForce = rescueForceRepository.getDefaultModel(taskId);
        //处理数据
        return resetSasualty(rescueForce);
    }

    //ss
    @Override
    public RescueForceEntity getModel1ById(String id) {
        RescueForceEntity rescueForce=rescueForceRepository.getModelById(id);
        return rescueForce;
    }

    private RescueForceEntity resetSasualty(RescueForceEntity rescueForce) {
        Map<String,String> areaMap =(Map<String,String>) JSONObject.parse(redisTemplate.opsForValue().get(FxfzConstants.CACHE_SYS_KEY  +"areaMap"));
        rescueForce.setProvince(areaMap.get(rescueForce.getProvince()));
        if(!"全部".equals(rescueForce.getCity())){
            StringBuffer buffer = new StringBuffer();
            List<String> cityList = Arrays.asList(rescueForce.getCity().split(","));
            for (String s : cityList) {
                buffer.append(areaMap.get(s)).append(",");
            }
            String cityName = buffer.toString();
            rescueForce.setCity(cityName.substring(0,cityName.length()-1));
        }
        return rescueForce;
    }


}
